package com.example.shaidemo;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class CarListFragment extends Fragment implements AdapterView.OnItemClickListener{

    private static final String TAG = "CarListFragment";
    private SimpleAdapter listItemAdapter; // 适配器
    ListView listView;
    SimpleAdapter simpleAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view =  inflater.inflate(R.layout.fragment_car_list, container, false);

        //进入筛选菜单
        TextView menuView = (TextView)view.findViewById(R.id.car_choose) ;
        menuView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent =new Intent(getActivity(),CarMenuActivity.class);
                startActivity(intent);
            }
        });

        listView = (ListView)view.findViewById(R.id.car_list_view);
        List<Map<String,String>> list = GetData.CarlList();
        simpleAdapter = new SimpleAdapter(getActivity(),list,
                R.layout.car_list_view,
                new String[]{"pic_1","one","two","pic_2","three","pic_3","four","five","six","pic_4"},
                new int[]{R.id.car_pic_1,R.id.car_start,R.id.car_price,R.id.car_pic_2,R.id.car_end,
                        R.id.car_pic_3,R.id.car_point,R.id.car_mileage,R.id.car_reliability,R.id.car_pic_4});
        listView.setAdapter(simpleAdapter);
        listView.setOnItemClickListener(this);

        return view;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Log.i(TAG, "onItemClick: parent=" + parent);
        Log.i(TAG, "onItemClick: view=" + view);
        Log.i(TAG, "onItemClick: position=" + position);
        Log.i(TAG, "onItemClick: id=" + id);

        //从ListView中获取选中数据
        HashMap<String,String> map = (HashMap<String, String>) listView.getItemAtPosition(position);
        String titleStr = map.get("one");
        String detailStr = map.get("two");
        Log.i(TAG, "onItemClick: titleStr=" + titleStr);
        Log.i(TAG, "onItemClick: detailStr=" + detailStr);

        Intent intent = new Intent(getActivity(),TravelContentActivity.class);
        startActivity(intent);
    }
}